/*
 * @lc app=leetcode.cn id=100 lang=javascript
 *
 * [100] 相同的树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {boolean}
 */

function TreeNode(val, left, right) {
    this.val = (val === undefined ? 0 : val)
    this.left = (left === undefined ? null : left)
    this.right = (right === undefined ? null : right)
}

// 思路
// 如果p, q都不存在，true
// p, q只有一个存在或者两者都存在但不相等，false
// 分别递归左子树和右子树

// 复杂度 O(m,n) O(m,n) m,n为两棵树的节点个数

var isSameTree = function (p, q) {
    if (!p && !q) {
        return true
    }
    if (!p || !q) {
        return false
    }
    if (p.val !== q.val) {
        return false
    }
    return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);


};
// @lc code=end


t1_r = new TreeNode(1, new TreeNode(2, null, null), new TreeNode(3, null, null))

t2_r = new TreeNode(1, new TreeNode(2, null, null), new TreeNode(3, null, null))

console.log(isSameTree(t1_r, t2_r))